package quanlyhochieu.hctrehan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import utils.DateUtils;

import dbutil.ConnectDB;

public class HoChieuTreHan {
	private String error;
	private int thoiGianTre;
	private List<HoChieu> listHC;
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public int getThoiGianTre() {
		return thoiGianTre;
	}
	public void setThoiGianTre(int thoiGianTre) {
		this.thoiGianTre = thoiGianTre;
	}
	int acc = 0;
	public List<HoChieu> displayAll(){
		acc = 1;
		java.util.Date currentDate = new java.util.Date();
		String sqlHC = "select *  from vitriluutru as VTLL, nhatkytranhanhc as NK, dsnguoidi as DS, doanra as DR, canhan as CN where VTLL.trangthainhan = 'G' and VTLL.trangthainhan = NK.thaotac and VTLL.sohc = NK.sohc and DS.sohc = VTLL.sohc and DS.sohc = NK.sohc and DR.madoan = DS.iddoanra and CN.cmnd = DS.cmnd";
		List<HoChieu> dsHC = new ArrayList<HoChieu>();
		ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(sqlHC);
		int stt = 0;
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();		
			for (int i = 0; i < array.size(); i++) {
				hash = (HashMap<String, Object>) array.get(i);
					String ngayve =  hash.get("ngayve").toString();
					String strNgayHetHan = DateUtils.layNgayHetHan(ngayve);
					java.sql.Date dateHetHan = java.sql.Date.valueOf(strNgayHetHan);
					Long dayBefor = DateUtils.daysBetween(currentDate,DateUtils.sqlDateToutilDate(dateHetHan));
					if(dayBefor < 0){
						//ngay hien tai lon hon ngay het han
						//luc nay nguoi nay da vi pham
					dayBefor = (-1)* dayBefor;
					Long logThoiGianTre = Long.parseLong(this.thoiGianTre+"");
					System.out.println("Thoi gian tre "+ logThoiGianTre);
					System.out.println("SQL "+ sqlHC);
						stt = stt + 1;
						HoChieu hoChieu = new HoChieu();
						hoChieu.setStt(stt);
						hoChieu.setCmnd(hash.get("cmnd").toString());
						hoChieu.setMaDoan(hash.get("madoan").toString());
						hoChieu.setSoHC(hash.get("sohc").toString());
						hoChieu.setTen(hash.get("tennguoidi").toString());
						String thoiGianDi = DateUtils.formatDate(hash.get("ngaydi").toString())+"-"+DateUtils.formatDate(hash.get("ngayve").toString());
						hoChieu.setThoiGianDi(thoiGianDi);
						hoChieu.setCoQuan(hash.get("coquan").toString());
						dsHC.add(hoChieu);
					}				
			}
				}
		this.setListHC(dsHC);
		return dsHC;
	}
	public List<HoChieu> getListHC() {
		if(acc == 0){
			this.displayAll();
		}
		acc = 0;
		return this.listHC;
	}
	public void setListHC(List<HoChieu> listHC) {
		this.listHC = listHC;
	}
	public String thongKe(){
		acc = 1;
		java.util.Date currentDate = new java.util.Date();
		//lay ds ho chieu co trang thai dang giao tu ban nhatkytranhanhc
		//tu danh sach hc do lay dc ds ma doan di
		//tu ds ma doan di biet dc ngay di va ngay ve cua nguoi giu ho chieu do
		//lay ngay ve cong voi 7 ngay 
			//neu truoc ngay hien tai this.thoiGianTre ngay thi add vao ds 
			//Neu sau hoac bang ngay hien tai thi hien thi thong bao chua toi thoi han tra ho chieu
		String sqlHC = "select *  from vitriluutru as VTLL, nhatkytranhanhc as NK, dsnguoidi as DS, doanra as DR, canhan as CN where VTLL.trangthainhan = 'G' and VTLL.trangthainhan = NK.thaotac and VTLL.sohc = NK.sohc and DS.sohc = VTLL.sohc and DS.sohc = NK.sohc and DR.madoan = DS.iddoanra and CN.cmnd = DS.cmnd";
		List<HoChieu> dsHC = new ArrayList<HoChieu>();
		ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(sqlHC);
		int stt = 0;
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();		
			for (int i = 0; i < array.size(); i++) {
				hash = (HashMap<String, Object>) array.get(i);
					String ngayve =  hash.get("ngayve").toString();
					String strNgayHetHan = DateUtils.layNgayHetHan(ngayve);
					java.sql.Date dateHetHan = java.sql.Date.valueOf(strNgayHetHan);
					Long dayBefor = DateUtils.daysBetween(currentDate,DateUtils.sqlDateToutilDate(dateHetHan));
					if(dayBefor < 0){
						//ngay hien tai lon hon ngay het han
						//luc nay nguoi nay da vi pham
					dayBefor = (-1)* dayBefor;
					Long logThoiGianTre = Long.parseLong(this.thoiGianTre+"");
					if(dayBefor == logThoiGianTre ){
						stt = stt + 1;
						HoChieu hoChieu = new HoChieu();
						hoChieu.setStt(stt);
						hoChieu.setCmnd(hash.get("cmnd").toString());
						hoChieu.setMaDoan(hash.get("madoan").toString());
						hoChieu.setSoHC(hash.get("sohc").toString());
						hoChieu.setTen(hash.get("tennguoidi").toString());
						String thoiGianDi = DateUtils.formatDate(hash.get("ngaydi").toString())+"-"+DateUtils.formatDate(hash.get("ngayve").toString());
						hoChieu.setThoiGianDi(thoiGianDi);
						hoChieu.setCoQuan(hash.get("coquan").toString());
						dsHC.add(hoChieu);
					}
					}
					
			}
				}
		if(stt == 0) this.setError("Không tồn tại hộ chiếu nộp trễ "+this.thoiGianTre+" ngày");
		else  this.setError("");
		this.setListHC(dsHC);
		return "hochieunoptrehan";
	}
	public String reset(){
		this.listHC = null;
		this.thoiGianTre = 0;
		this.setError("");
		return "hochieunoptrehan";
	}
	
}
	
	
 

